%{
    This file is part of StemCellQC, a video bioinformatics software
    toolkit for analysis of phase contrast microscopy videos.
    Copyright 2013-2015 Vincent On. [vincenton001-at-gmail.com]

    StemCellQC is free software: you can redistribute it and/or 
    modify it under the terms of the GNU General Public License as 
    published by the Free Software Foundation, either version 3 of the 
    License, or (at your option) any later version.

    StemCellQC is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with StemCellQC.  If not, see <http://www.gnu.org/licenses/>.
%}

% function creates 3 classifers and tests the classification rates with cross validation: SVM, K-Nearest
% Neighbors, and Naive Bayes.

function results = hESC_build_classifier( features , run_settings, multipler_value )

runs = run_settings.NumRuns;
k_fold = run_settings.K_Folds;
k_neighbors = 3;
n_frames = run_settings.Mov_Avg_Int;

%removes features that won't be used in classifier
%stat values
%1 - area
%2 - perimeter
%3 - centroid x
%4 - centroid y
%5 - extent
%6 - solidity
%7 - orientation
%8 - major axis length
%9 - minor axis length
%10 - eccentricity
%11 - min radius
%12 - max radius
%13 - avg radius
%14 - avg intensity
%15 - Max intensity
%16 - min intensity
%17 - bright area ratio
%18 - # of protrusions
%19 - Ratio of protrusion area
%20 - change in area
%21 - change in perimeter
%22 - change in centroid
%23 - mean squared displacement


% multipler_value = [ 1; multipler_value ];
% multipler_value( remove_c ) = [];
%% load data and labels
% get the directory
Load_classifier_data_labels;


%cross validation indices
ER_svm = [];
ER_knn = [];
ER_nb = [];


hWaitbar = waitbar( 0, 'Test Runs' );
close( hWaitbar );

for j = 1:runs
    
    hWaitbar = waitbar( j / runs, ...
        'Test Runs' );
    close( hWaitbar );
    
    indices = crossvalind('Kfold', length(labels), k_fold);
    
    cp_svm = classperf(labels);
    cp_knn = classperf(labels);
    cp_nb = classperf(labels);
    
    for i = 1 : k_fold
        test = (indices == i);
        train = ~test;
        
        %SVM
        %construct classifier from data and labels
        SVMStruct = svmtrain(samples(train,:),labels(train,:));
        
        %classify
        svm_class = svmclassify(SVMStruct, samples(test,:));
        
        classperf(cp_svm,svm_class,test);
        
        %KNN
        
        knn_class = knnclassify( samples(test,:) , samples(train,:) , labels(train,:) , k_neighbors );
        
        classperf(cp_knn,knn_class,test);
        
        
        %NaiveBayes
        nb = NaiveBayes.fit(samples(train,:),labels(train,:));
        
        nb_class = predict(nb,samples(test,:));
        
        classperf(cp_nb,nb_class,test);
        
    end
    
    ER_svm = [ER_svm; cp_svm.ErrorRate];
    ER_knn = [ER_knn; cp_knn.ErrorRate];
    ER_nb = [ER_nb; cp_nb.ErrorRate];
    
    
end

CR_svm = ( 1 - ER_svm ) * 100;
CR_knn = ( 1 - ER_knn ) * 100;
CR_nb = ( 1 - ER_nb ) * 100;

results = [ mean(CR_svm) std(CR_svm);
    mean(CR_knn) std(CR_knn);
    mean(CR_nb) std(CR_nb) ];
end


